using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraPivotGrid;
using DevExpress.Utils;
using DevExpress.Data.PivotGrid;
using Marlimer.Common;
using Marlimer.Logical.Comercial;

namespace Marlimer.Comercial.Reportes.Formularios
{
    public partial class frm_DevolucionXMotivo : Form
    {
        lcom_Ventas lcom_ventas = new lcom_Ventas();

        public frm_DevolucionXMotivo()
        {
            InitializeComponent();
        }

        private void frm_AvancesXSKU_Load(object sender, EventArgs e)
        {
            dtpFecIni.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            dtpFecFin.Value = DateTime.Today;
            configurarPivot(); 
        }

        private void configurarPivot()
        {
            pvtDetalles.Fields.Clear();

            PivotGridField fieldCol1 = new PivotGridField();
            PivotGridField fieldCol2 = new PivotGridField();

            PivotGridField fieldRow1 = new PivotGridField();
            PivotGridField fieldRow2 = new PivotGridField();
            PivotGridField fieldRow3 = new PivotGridField();

            PivotGridField fieldData1 = new PivotGridField();
            PivotGridField fieldData2 = new PivotGridField();

            fieldRow1.Area = PivotArea.RowArea;
            fieldRow1.AreaIndex = 0;
            fieldRow1.FieldName = "ccod_zona";
            fieldRow1.Caption = "Zona";
            fieldRow1.Width = 150;

            fieldRow2.Area = PivotArea.RowArea;
            fieldRow2.AreaIndex = 1;
            fieldRow2.FieldName = "ccod_ruta";
            fieldRow2.Caption = "Ruta";
            fieldRow2.Width = 80;

            fieldRow3.Area = PivotArea.RowArea;
            fieldRow3.AreaIndex = 2;
            fieldRow3.FieldName = "vendedor";
            fieldRow3.Caption = "Vendedor";
            fieldRow3.Width = 150;

            fieldCol1.Area = PivotArea.ColumnArea;
            fieldCol1.AreaIndex = 0;
            fieldCol1.FieldName = "cdes_motgen";
            fieldCol1.Caption = "General";
            fieldCol1.Width = 80;

            fieldCol2.Area = PivotArea.ColumnArea;
            fieldCol2.AreaIndex = 2;
            fieldCol2.FieldName = "cdes_motanul";
            fieldCol2.Caption = "Motivo";
            fieldCol2.Width = 80;

            fieldData1.Area = PivotArea.DataArea;
            fieldData1.AreaIndex = 2;
            fieldData1.FieldName = "ntot_docs";
            fieldData1.Caption = "Docs";
            fieldData1.SummaryType = PivotSummaryType.Sum;
            fieldData1.CellFormat.FormatType = FormatType.Numeric;
            fieldData1.CellFormat.FormatString = "0.00";

            fieldData2.Area = PivotArea.DataArea;
            fieldData2.AreaIndex = 2;
            fieldData2.FieldName = "ntot_soles";
            fieldData2.Caption = "Soles";
            fieldData2.SummaryType = PivotSummaryType.Sum;
            fieldData2.CellFormat.FormatType = FormatType.Numeric;
            fieldData2.CellFormat.FormatString = "0.00";

            pvtDetalles.Fields.AddRange(new DevExpress.XtraPivotGrid.PivotGridField[] { fieldCol1, fieldCol2, fieldRow1, fieldRow2, fieldRow3, fieldData1, fieldData2 });

        }

        private void tstGenComandos_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int indice = -1;
            if (sender.GetType().Name == "ToolStrip")
            {
                indice = tlbComandos.Items.IndexOf(e.ClickedItem);
            }
            else
            {
                indice = mnuComandos.DropDownItems.IndexOf(e.ClickedItem);
            }

            switch (indice)
            {
                case 0:     //nuevaBusqueda
                    {
                        nuevaBusqueda();
                        break;
                    }
                case 1:     //ejecutar busqueda
                    {
                        buscar();
                        break;
                    }
                case 2:
                    {
                        exportar();
                        break; 
                    }
            }
        }

        private void exportar()
        {
            //configurar el cuadro de dialogo
            resulOMsgError resSave = Util.showDialogSave(this, "Guardar como...", "Archivos de Excel (*.xls)|*.xls");

            if (resSave.resul)
            {
                try
                {
                    pvtDetalles.ExportToXls((string)resSave.clase);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void buscar()
        {
            DataTable dtDev = lcom_ventas.DevolucionXMotivo(dtpFecIni.Value, dtpFecFin.Value, 0, 0, "*");
            //agregar dos columnas
            dtDev.Columns.Add("ntot_docs", Type.GetType("System.Int32"));
            dtDev.Columns.Add("ntot_soles", Type.GetType("System.Double"));
            dtDev.Columns["ntot_docs"].Expression = "tot_puntosD + tot_puntosDP";
            dtDev.Columns["ntot_soles"].Expression = "ntot_solesD + ntot_solesDP";
            pvtDetalles.DataSource = dtDev; 
        }

        private void nuevaBusqueda()
        {
            pvtDetalles.DataSource = null;
            configurarPivot(); 
        }

    }
}